package com.link.play.fit.ai.ui.main.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.CalendarStatusData
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.ui.view.ArcTextView
import com.link.play.fit.ai.ui.view.CircleStatusTextView
import com.link.play.fit.ai.utils.DateEx.getDayOfMonthByLocal
import com.link.play.fit.ai.utils.WeekDatesUtil.getFirstLetterOfWeek
import java.time.ZoneId
import java.util.Calendar


class CalendarAdapter(
    private val dates: List<CalendarStatusData>,
    val context: Context,
    val onclick: (String, Int) -> Unit
) :
    RecyclerView.Adapter<CalendarAdapter.ViewHolder>() {
    private var mPosition = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.list_item_layout, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val date = dates[position]
//        if (date.consumeCaloriesDaily >= date.caloriesDaily && date.caloriesDaily!=-1) {
//            holder.dayText.setColorStatus(2)
//            holder.dayText.setState(1)
//        }else if(date.consumeCaloriesDaily < date.caloriesDaily){
//            holder.dayText.setColorStatus(1)
//            holder.dayText.setState(1)
//        }    android:background="@drawable/bg_style_gree_r360"
        // 将 Date 转换为 Instant，再转换为 LocalDate
        val localDate = date.date.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDate()
//        holder.circlesText.text = localDate.dayOfMonth.toString()
//        holder.progressbar.max = date.caloriesDaily
        holder.circlesText.text = localDate.dayOfMonth.toString()
        holder.arcTextView.text = getFirstLetterOfWeek(getDayOfMonthByLocal(localDate)).toString()


        if (mPosition == position) {
            holder.contentLayout.background = context.getDrawable(R.drawable.bg_style_gree_r360)
            holder.circlesText.background = context.getDrawable(R.drawable.bg_style_white_r360)
            holder.circlesText.setStatus(CircleStatusTextView.Status.NONE)
            holder.circlesText.setTextColor(context.getColor(R.color.black_11))
            holder.arcTextView.setTextColor(context.getColor(R.color.white))
        } else {
            holder.contentLayout.background = null
            if (position == dates.size - 2) {
                holder.circlesText.background = context.getDrawable(R.drawable.bg_style_gree_r360)
                holder.circlesText.setTextColor(context.getColor(R.color.white))
            } else {
                holder.circlesText.background = context.getDrawable(R.drawable.bg_style_white_r360)
                if (!date.isInit) {
                    holder.circlesText.setStatus(CircleStatusTextView.Status.NONE)
                    holder.circlesText.setTextColor(context.getColor(R.color.color_A3A4A6))
                    holder.arcTextView.setTextColor(context.getColor(R.color.color_A3A4A6))
                } else {
//                    if (position != dates.size - 2) {
                        if (date.consumeCaloriesDaily >= date.caloriesDaily) {
                            holder.circlesText.setProgress(
                                date.caloriesDaily.toFloat(),
                                date.caloriesDaily.toFloat()
                            )
                            holder.circlesText.setStatus(CircleStatusTextView.Status.GREEN_CIRCLE)
                        } else {
                            holder.circlesText.setProgress(
                                date.consumeCaloriesDaily.toFloat(),
                                date.caloriesDaily.toFloat()
                            )
                            holder.circlesText.setStatus(CircleStatusTextView.Status.RED_ARC)
                        }
                        holder.circlesText.setTextColor(context.getColor(R.color.black_11))
                        holder.arcTextView.setTextColor(context.getColor(R.color.black_11))
//                    }
                }
//                holder.circlesText.setStatus(CircleStatusTextView.Status.NONE)
//                holder.circlesText.setTextColor(context.getColor(R.color.color_A3A4A6))
//                holder.circlesText.background = context.getDrawable(R.drawable.bg_style_white_r360)
//                holder.arcTextView.setTextColor(context.getColor(R.color.color_A3A4A6))
            }
        }

        holder.contentLayout.setOnClickListener {
            if (position == dates.size - 1) return@setOnClickListener
            val time = getDayOfMonthByLocal(localDate)
            onclick.invoke(time, position)
            mPosition = position
            notifyDataSetChanged()
        }

    }

    override fun getItemCount(): Int = dates.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val arcTextView: TextView = itemView.findViewById(R.id.arcTextView)
        val contentLayout: LinearLayout = itemView.findViewById(R.id.contentLayout)
        val circlesText: CircleStatusTextView = itemView.findViewById(R.id.circlesText)

    }

    fun notifyList(position: Int) {
        mPosition = position
        notifyDataSetChanged()
    }
}